package com.caszx.chargingpile.fragment.station;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONException;
import com.app.base.adapter.BaseClickRecyclerAdapter;
import com.app.base.adapter.BaseHeadFootAdapter;
import com.app.jsonparse.JSONParse;
import com.app.utils.DividerGridItemDecoration;
import com.caszx.chargingpile.R;
import com.caszx.chargingpile.entity.EntityTabFeeScheme;
import com.caszx.chargingpile.entity.EntityTabPlie;
import com.caszx.chargingpile.entity.EntityTabStationDetail;
import com.caszx.chargingpile.holder.reservate.OftenUseHolder;
import com.caszx.chargingpile.holder.station.TabListHolder;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import github.chenupt.dragtoplayout.AttachUtil;

/**电桩列表*/
public class TabListFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private BaseHeadFootAdapter<EntityTabPlie> mRecyclerAdapter;
    private List<EntityTabPlie> mList;
    public static TabListFragment newInstance(String plieList) {
        TabListFragment fragment = new TabListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("PLIE",plieList);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            String  stringFeeScheme = getArguments().getString("PLIE");
            if(stringFeeScheme!=null){
                try {
                    mList = JSONParse.parseArray(stringFeeScheme, EntityTabPlie.class);
                }catch (JSONException e){

                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view  = inflater.inflate(R.layout.common_recyclerview, container, false);
        initRecyclerView(view);
        return view;
    }

    private void initRecyclerView(View view) {

        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mRecyclerView.setBackgroundResource(R.color.gray3);

        LinearLayoutManager layoutManager = new LinearLayoutManager( getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);
        DividerGridItemDecoration decoration = new DividerGridItemDecoration(getActivity());
        decoration.setDividerPadding(16, 16);
        mRecyclerView.addItemDecoration(decoration);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerAdapter = new BaseHeadFootAdapter<>(R.layout.item_tab_station,TabListHolder.class);
        mRecyclerView.setAdapter(mRecyclerAdapter);
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                EventBus.getDefault().post(AttachUtil.isRecyclerViewAttach(recyclerView));
            }
        });

        mRecyclerAdapter.setDatas(mList);
    }
}
